﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ExcelFileQueryTool
{
    public partial class frmExcelWorkLogFilterSetting : Form
    {
        public string FilterString { get; private set; }

        public frmExcelWorkLogFilterSetting(string keyword = null)
        {
            InitializeComponent();

            var config = SpecialKeywordExcelWorkLogFilter.ParseFilterConfig(keyword);

            switch (config.MatchMethod)
            {
                case ExcelWorkLogFilterMatchMethod.Equal:
                    radioEqual.Checked = true;
                    break;
                case ExcelWorkLogFilterMatchMethod.Like:
                default:
                    radioLike.Checked = true;
                    break;
            }

            switch (config.CombineMethod)
            {
                case ExcelWorkLogFilterCombineMethod.And:
                    radioAnd.Checked = true;
                    break;
                case ExcelWorkLogFilterCombineMethod.Or:
                default:
                    radioOr.Checked = true;
                    break;
            }

            foreach (var fieldCfg in config.FieldConfigs)
            {
                var fieldName = fieldCfg.Field.ToString();
                ((CheckBox)this.Controls["chk" + fieldName]).Checked = true;
                ((TextBox)this.Controls["txt" + fieldName]).Text = fieldCfg.Value;
            }

            FilterString = keyword;
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            ExcelWorkLogFilterConfig config = new ExcelWorkLogFilterConfig();

            if (chkType.Checked && string.IsNullOrWhiteSpace(txtType.Text) == false)
            {
                config.FieldConfigs.Add(new ExcelWorkLogFilterFieldConfig
                {
                    Field = ExcelWorkLogFilterField.Type,
                    Value = txtType.Text.Trim()
                });
            }

            if (chkName.Checked && string.IsNullOrWhiteSpace(txtName.Text) == false)
            {
                config.FieldConfigs.Add(new ExcelWorkLogFilterFieldConfig
                {
                    Field = ExcelWorkLogFilterField.Name,
                    Value = txtName.Text.Trim()
                });
            }

            if (chkItem.Checked && string.IsNullOrWhiteSpace(txtItem.Text) == false)
            {
                config.FieldConfigs.Add(new ExcelWorkLogFilterFieldConfig
                {
                    Field = ExcelWorkLogFilterField.Item,
                    Value = txtItem.Text.Trim()
                });
            }

            if (chkDriver.Checked && string.IsNullOrWhiteSpace(txtDriver.Text) == false)
            {
                config.FieldConfigs.Add(new ExcelWorkLogFilterFieldConfig
                {
                    Field = ExcelWorkLogFilterField.Driver,
                    Value = txtDriver.Text.Trim()
                });
            }

            if (chkNote.Checked && string.IsNullOrWhiteSpace(txtNote.Text) == false)
            {
                config.FieldConfigs.Add(new ExcelWorkLogFilterFieldConfig
                {
                    Field = ExcelWorkLogFilterField.Note,
                    Value = txtNote.Text.Trim()
                });
            }

            if (chkPay.Checked && string.IsNullOrWhiteSpace(txtPay.Text) == false)
            {
                config.FieldConfigs.Add(new ExcelWorkLogFilterFieldConfig
                {
                    Field = ExcelWorkLogFilterField.Pay,
                    Value = txtPay.Text.Trim()
                });
            }

            if (config.FieldConfigs.Count > 0)
            {
                if (radioAnd.Checked)
                    config.CombineMethod = ExcelWorkLogFilterCombineMethod.And;
                else
                    config.CombineMethod = ExcelWorkLogFilterCombineMethod.Or;

                if (radioEqual.Checked)
                    config.MatchMethod = ExcelWorkLogFilterMatchMethod.Equal;
                else
                    config.MatchMethod = ExcelWorkLogFilterMatchMethod.Like;

                this.FilterString = (new SpecialKeywordExcelWorkLogFilter()).GenerateKeyword(config);
            }
            else
            {
                this.FilterString = string.Empty;
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void txtName_Leave(object sender, EventArgs e)
        {
            ProcessRelativeCheckBoxWhenTextBoxFocusLeave(txtName, chkName);
        }

        private void txtItem_Leave(object sender, EventArgs e)
        {
            ProcessRelativeCheckBoxWhenTextBoxFocusLeave(txtItem, chkItem);
        }

        private void txtDriver_Leave(object sender, EventArgs e)
        {
            ProcessRelativeCheckBoxWhenTextBoxFocusLeave(txtDriver, chkDriver);
        }

        private void txtType_Leave(object sender, EventArgs e)
        {
            ProcessRelativeCheckBoxWhenTextBoxFocusLeave(txtType, chkType);
        }

        private void txtNote_Leave(object sender, EventArgs e)
        {
            ProcessRelativeCheckBoxWhenTextBoxFocusLeave(txtNote, chkNote);
        }

        private void txtPay_Leave(object sender, EventArgs e)
        {
            ProcessRelativeCheckBoxWhenTextBoxFocusLeave(txtPay, chkPay);
        }

        static void ProcessRelativeCheckBoxWhenTextBoxFocusLeave(TextBox txt, CheckBox chk)
        {
            chk.Checked = (string.IsNullOrWhiteSpace(txt.Text) == false);
        }

        private void txtName_Enter(object sender, EventArgs e)
        {
            chkName.Checked = true;
        }

        private void txtItem_Enter(object sender, EventArgs e)
        {
            chkItem.Checked = true;
        }

        private void txtDriver_Enter(object sender, EventArgs e)
        {
            chkDriver.Checked = true;
        }

        private void txtType_Enter(object sender, EventArgs e)
        {
            chkType.Checked = true;
        }

        private void txtNote_Enter(object sender, EventArgs e)
        {
            chkNote.Checked = true;
        }

        private void txtPay_Enter(object sender, EventArgs e)
        {
            chkPay.Checked = true;
        }
    }
}
